package com.hznu.stmanage.utils;

import com.hznu.stmanage.entity.Manager;
import com.hznu.stmanage.entity.Student;
import com.hznu.stmanage.entity.Teacher;
import com.hznu.stmanage.utils.cryptolib.CryptoApp;
import com.hznu.stmanage.utils.cryptolib.PsdProcRlt;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by J_Vino on 16/3/14.
 * 加密明文密码
 */
@Service
public class PasswordEncrypt {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

    @Value("${algorithmName}")
    private String algorithmName;
    @Value("${hashIterations}")
    private int hashIterations;

    public RandomNumberGenerator getRandomNumberGenerator() {
        return randomNumberGenerator;
    }

    public String getAlgorithmName() {
        return algorithmName;
    }

    public int getHashIterations() {
        return hashIterations;
    }

    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
        this.randomNumberGenerator = randomNumberGenerator;
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public void encryptStudent(Student student) {
            PsdProcRlt r = null;
            //对原始密码进行加密处理,处理后得到salt及密文
        try {
            r = CryptoApp.PwdProcess(student.getPassword().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        student.setSalt(Util.bytesToHexString(r.getSalt()));//设置salt值
        student.setPassword(Util.bytesToHexString(r.getValue()));//设置new password

    }
    public void encryptTeacher(Teacher teacher) {

            PsdProcRlt r = null;
            //对原始密码进行加密处理,处理后得到salt及密文
        try {
            r = CryptoApp.PwdProcess(teacher.getPassword().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        teacher.setSalt(Util.bytesToHexString(r.getSalt()));//设置salt值
        teacher.setPassword(Util.bytesToHexString(r.getValue()));//设置new password

    }
    public void encryptManager(Manager manager) {
            PsdProcRlt r = null;
            //对原始密码进行加密处理,处理后得到salt及密文
        try {
            r = CryptoApp.PwdProcess(manager.getPassword().getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        manager.setSalt(Util.bytesToHexString(r.getSalt()));//设置salt值
        manager.setPassword(Util.bytesToHexString(r.getValue()));//设置new password

    }

}
